package com.example.boot.service

import org.junit.jupiter.api.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.InjectionPoint
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Component

@SpringBootTest
class ExceptionTest {

    @Autowired
    private lateinit var ledgerAccountExceptionUtil: LedgerAccountExceptionUtil
    
    @Test
    fun notThrowRightNow() {
        ledgerAccountExceptionUtil.record(ExceptionName.LEDGER_ACCOUNT_NOT_FOUND, "some msgs")
    }
}

@Component
class LedgerAccountExceptionUtil(private val logger: Logger) {

    fun record(exp: ExceptionName, msg: String) {
        logger.error(msg)
        when (exp) {
            ExceptionName.LEDGER_ACCOUNT_NOT_FOUND -> throw LedgerAccountNotFoundException(msg)
        }
    }
}

enum class ExceptionName {
    LEDGER_ACCOUNT_NOT_FOUND,
}

class LedgerAccountNotFoundException(msg: String) : Exception(msg) {

}

@Configuration
class LoggerConfig {
    @Bean
    @Scope("prototype")
    fun logger(injectionPoint: InjectionPoint): Logger {
        return LoggerFactory.getLogger(
            injectionPoint.methodParameter?.containingClass ?: injectionPoint.field?.declaringClass
        )
    }
}

//open class LedgerAccountBaseException() : Exception() {
//    private val logger: Logger = LoggerFactory.getLogger(LedgerAccountBaseException::class.java)
//
//    constructor(message: String) : this() {
//        logger.error(message)
//    }
//}


//class LedgerAccountNotFoundException(message: String) : LedgerAccountBaseException(message) {
//
//}
//
//class OtherException(message: String) : LedgerAccountBaseException(message) {
//
//}